﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace iver.Controllers
{
    public class homeSetController : ApiController
    {
        #region 读写apconfig-首页管理
        /// <summary>
        /// 写入配置
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ResResult writeConfig([FromBody] ReqKeyValuePair req)
        {
            Entity.AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            //查询是否存在
            DBEntity.iverDB db = new DBEntity.iverDB();
            DBEntity.apConfig config = db.apConfig.FirstOrDefault(u => u.name == req.name);
            if (config == null)
            {
                db.apConfig.Add(new DBEntity.apConfig
                {
                    name = req.name,
                    jsonString = req.jsonString,
                });
                db.SaveChanges();
                Help.StaticApConfig();
                return new ResResult
                {
                    success = true,
                    rescode = ResCode.SuccessAdd,
                };
            }
            else
            {
                config.jsonString = req.jsonString;
                db.SaveChanges();
                Help.StaticApConfig();
                return new ResResult
                {
                    success = true,
                    rescode = ResCode.SuccessEdit,
                };
            }



        }
        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public ResResult ReadConfig([FromUri] ReqKeyValuePair req)
        {
            DBEntity.iverDB db = new DBEntity.iverDB();
            DBEntity.apConfig config = db.apConfig.FirstOrDefault(u => u.name == req.name);
            if (config == null)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }
            return new ResResult
            {
                success = true,
                result = config.jsonString,
            };
        }
        #endregion

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResResult uploadImg()
        {
            Entity.AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            return Help.uploadfile("homeImg", 3);
        }
    }
}